package com.data.center.controller;

import cn.hutool.core.bean.BeanUtil;
import com.data.center.dto.OrganizationDto;
import com.data.center.dto.StaffDto;
import com.data.center.entity.Staff;
import com.data.center.exception.ResultBody;
import com.data.center.service.StaffService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2023-12-05 03:29:24
 */
@RestController
@RequestMapping("/staff")
@Slf4j
@Api(tags = "业务人员")
public class StaffController {

    @Autowired
    StaffService staffService;

    @PostMapping("/findStaff")
    public ResultBody findStaff(@RequestBody StaffDto staff) {
        try {
            return ResultBody.success(staffService.findStaff(staff));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/addStaff")
    public ResultBody addStaff(@RequestBody StaffDto staff) {
        try {
            return ResultBody.success(staffService.addStaff(staff));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/forceAddStaff")
    public ResultBody forceAddStaff(@RequestBody StaffDto staff) {
        try {
            return ResultBody.success(staffService.forceAddStaff(staff));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/updateStaff")
    public ResultBody updateStaff(@RequestBody StaffDto staffDto) {
        log.info("增量信息" + staffDto + staffDto.getKeyWithAdditionalInformation());
        Staff staff = staffService.getById(staffDto.getStaffId());
        if (staff == null) {
            return ResultBody.error("人员不存在");
        }
        try {
            return ResultBody.success(staffService.updateStaff(BeanUtil.copyProperties(staff, StaffDto.class), staffDto));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/logicalDelete")
    public  ResultBody logicalDelete(@RequestBody StaffDto staff) {
        try {
            return ResultBody.success(staffService.logicalDelete(staff.getStaffId()));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/physicalDelete")
    public  ResultBody physicalDelete(@RequestBody StaffDto staff) {
        try {
            return ResultBody.success(staffService.physicalDelete(staff.getStaffId()));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }

    @PostMapping("/merge")
    public  ResultBody merge(@RequestBody StaffDto staffDto, @RequestBody StaffDto deletedStuff) {
        try {
            return ResultBody.success(staffService.merge(staffDto, deletedStuff));
        } catch (Exception e) {
            return ResultBody.error(e.toString());
        }
    }
}
